package com.bob.springmvcmessageresolverdemo.controller;

import com.bob.springmvcmessageresolverdemo.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("demo")
public class DemoController {

    /**
     * 前端在传参数的实收，只要以FormData对象传输就好，Content-Type为：application/x-www-form-urlencoded或者form-data
     * 值得注意的是：当Content-Type为：pplication/x-www-form-urlencoded，参数是要拼接到url后面的，即?username=zhangsan&password=1213
     * @param userEntity
     * @return
     */
    @RequestMapping("form_data")
    public String formData(UserEntity userEntity) {
       return "获得参数:" +  userEntity.toString();
    }

    /**
     * 这种两个参数的时候，用上面的formdata的方式接受是可以的
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("multi_params_form_data")
    public String multiParamsFormData(String username, String password) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        return "获得参数:" +  userEntity.toString();
    }

    /**
     * 当前端要以Json形式传参数是，需要在参数前面加上@RequestBody注解，表示用该字段来接受前端的参数
     * 这时候前端传过来的请求头中：Content-Type必须要是：application/json，否则后端会报错，这时候前端的数据是json对象形式放在body中的
     * @param userEntity
     * @return
     */
    @RequestMapping("json")
    public String json(@RequestBody UserEntity userEntity) {
        return "获得参数:" +  userEntity.toString();
    }

    @RequestMapping("multi_params_json")
    public String json(String username, String password) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        return "获得参数:" +  userEntity.toString();
    }
}
